var city='';
var provinceVal='';

$(function(){
	
	//从选择医院界面过来，直接选择医生
	var hospitalIdSel=$("#wx_hospital_id_sel").val();
	if(""!=hospitalIdSel)
	{
		createSelDoctor(hospitalIdSel);
		$("#divSelHospital").hide();
		$("#divDoctorDess").css({"margin-top":"10px"});
	}
	else
	{
		//构建省级区域选择框
		createSelProvince(); 
	}
});

/**
 * 根据维度、经度获取地点名称
 * @param latitude 维度
 * @param longitude 经度
 */
function getLocationName(latitude,longitude)
{
	//根据经纬度获取当前城市
	 $.ajax({  
	     url:"http://api.map.baidu.com/geocoder/v2/?location="+latitude+","+longitude+"&output=json&ak=Xt6gg6jdOUqKdnORN9L7bxIq",  
	     dataType:'jsonp',  
	     data:'',  
	     jsonp:'callback',  
	     success:function(result) {
	    	 provinceVal=result.result.addressComponent.province;
	    	 city =result.result.addressComponent.city;
	    	 
	    	//构建省级区域选择框
	    	createSelProvince(); 
	     }
	  }); 
}


//构建问题列表
function createQuestionList(userId)
{
	$.ajax({
        async : false,  
        cache : false,
        type : 'post',  
        url : '../weixin/qusetionnaire/query',
        data : {  
            'userId' : userId
        },
        success:function(data){
        	var questionnairesStr="";
        	for(var i=0;i<data.length;i++)
        	{
        		var questionnaire=data[i];
        		questionnairesStr+="<div style='height: 30px;'>";
        		questionnairesStr+="<a href='../weixin/qusetionnaire/goto?questionnaireId="+questionnaire.id+"'>"+questionnaire.title+"</a>";
        		questionnairesStr+="</div>";
        	}
        	
        	$("#divQuestionnaireList").html(questionnairesStr);
        }
    });
}

/**
 * 选择医生聊天
 * @param userId 医生用户Id
 * @param nickname 昵称
 */
var selDoctorUserId="";
var selDoctorNickname="";
function selDoctor()
{
	if(null==selDoctorUserId||selDoctorUserId==""||undefined==selDoctorUserId)
	{
		alert("请先选择医生");
		return false;
	}
	var userId=selDoctorUserId;
	var nickname=selDoctorNickname;
	if(confirm("确认选择【"+nickname+"】医生吗？"))
	{
		var openid=$("#wx_open_id").val();
		$.ajax({
	        async : false,  
	        cache : false,
	        type : 'post',  
	        url : '../weixin/doctor/select',
	        data : {  
	            'userId' : userId,
	            'nickname':nickname,
	            'openid':openid
	        },
	        success:function(data){
	        	if(data.result==true)
	        	{
	        		wx.closeWindow();
	        	}
	        }
	    });
	}
}

//构建省份选择框
function createSelProvince()
{
	$.ajax({
        cache : true,
        type : 'post',  
        url : '../weixin/location/grade/next',
        data : {  
            'parentId' : '0',
        },
        success:function(data){
        	var provinces=data;
        	var strProvince="<option value=''>请选择省份</option>";
        	for(var n=0;n<provinces.length;n++)
        	{
        		var province=provinces[n];
        		var selProvince="";
        		
        		if(provinceVal==province.name&&provinceVal!='')
        		{
        			selProvince="selected='selected'";
        		}else if('重庆市'==province.name){
        			selProvince="selected='selected'";
        		}
        		 
        		strProvince+="<option value='"+province.id+"' "+selProvince+">"+province.name+"</option>";
        	}
        	$("#selProvince").html(strProvince);
        	
        	//构建城市选择
        	createSelCity($("#selProvince").val());
        }
    });	
}

//构建城市区域选择框
function createSelCity(parentId)
{
	$.ajax({
        cache : true,
        type : 'post',  
        url : '../location/grade/next',
        data : {  
            'parentId': parentId,
        },
        success:function(data){
        	var locations=data;
        	var strLocation="<option value=''>请选择城市</option>";
        	for(var n=0;n<locations.length;n++)
        	{
        		var location=locations[n];
        		var selLoc="";
        		
        		if(city==location.name){
        			selLoc="selected='selected'";
        		}else if(n==0)
        		{
        			selLoc="selected='selected'";
        		}
        		strLocation+="<option value='"+location.id+"' "+selLoc+">"+location.name+"</option>";
        	}
        	$("#selCity").html(strLocation);
        	
        	//构建医院选择
        	createSelHospital($("#selCity").val());
        }
    });	
}

//构建区域选择框
function createSelHospital(locationId)
{
		$.ajax({
	        cache : true,
	        type : 'post',  
	        url : '../weixin/hospital/list',
	        data : {  
	            'locationId': locationId,
	        },
	        success:function(data){
	        	var hospitals=data;
	        	var strHospital="<option value=''>请选择医院</option>";
	        	for(var n=0;n<hospitals.length;n++)
	        	{
	        		var hospital=hospitals[n];
	        		var selHospital="";
	        		if(n==(hospitals.length-1))
	        		{
	        			selHospital="selected='selected'";
	        		}
	        		strHospital+="<option value='"+hospital.id+"' "+selHospital+">"+hospital.name+"</option>";
	        	}
	        	$("#selHospital").html(strHospital);
	        	
	        	//构建医生选择
	        	createSelDoctor($("#selHospital").val());
	        }
	    });	
}

//构建医生选择
function  createSelDoctor(hospitalId)
{
	if(null==hospitalId||undefined==hospitalId||""==hospitalId)
	{
		hospitalId="99999999";
	}
	
	$.ajax({
        cache : true,
        type : 'post',  
        url : '../weixin/doctor/list',
        data : {  
            'hospitalId': hospitalId,
        },
        success:function(data){
        	var doctors=data;
        	var strDoctors="";
        	var strDoctorDes="";//用户描述
        	$("#ulDoctorSel").html("");
        	$("#divDoctorDess").html("");
        	for(var n=0;n<doctors.length;n++)
        	{
        		var doctor=doctors[n];
        		strDoctors+="<li class='li"+doctor.userId+"' title='liDoctor'>";
        		strDoctors+="<div class='box box21'>";
        		strDoctors+="<a href=\"javascript:showDoctorDes('"+doctor.userId+"','"+doctor.name+"');\" >";
        		strDoctors+="<img alt='"+doctor.name+"\' src='"+doctor.img+"' width='"+imgWidth+"' height='"+(imgWidth*1.5)+"'>";
        		strDoctors+="</a>";
        		strDoctors+="</div>";
        		strDoctors+="</li>";
        		
        		//构建医生简介内容
        		var docDesDisplay="display: none;";
        		if(n==0)
        		{
        			docDesDisplay="display: block;";
        		}
        		strDoctorDes+="<div name='divDoctorDes' class='divDoctorDes' id='divDoctorDes"+doctor.userId+"' style='border: 1px;"+docDesDisplay+"'>";
        		strDoctorDes+=doctor.introduction;
        		strDoctorDes+="<br/><br/>";
        		strDoctorDes+="</div>";
        		
        	}
        	$("#ulDoctorSel").html(strDoctors);
        	$("#divDoctorDess").html(strDoctorDes);
        	
        	//初始化图片显示位置
        	lrtkInit();
        }
    });	
	$(".swaplist").css("left",imgNum*(imgWidth+imgSpa)+"px");
}

//显示医生介绍
function showDoctorDes(doctorUserId,doctorName)
{
	 selDoctorUserId=doctorUserId;
	 selDoctorNickname=doctorName;
	
	 $("div[name='divDoctorDes']").css("display","none");
	 $("#divDoctorDes"+doctorUserId).css("display","block");
	 $("li[title='liDoctor'] div").css("box-shadow","0 1px 4px rgba(0, 0, 0, 0.3), 0 0 40px rgba(0, 0, 0, 0.1) inset");
	 $(".li"+doctorUserId+" div").css("box-shadow","0 1px 8px rgba(255, 0, 0, 0.3), 0 0 40px rgba(255, 0, 0, 0.4) inset");
}
